/**
 * 这是一个逻辑类（我方）
 * 控制游戏和绑定各种键盘事件
 */
import {Game} from './game.js';

class Local{
	constructor(socket){
		this.dom = {
			gameBox: document.querySelector('#local-game-box'),
			nextBox: document.querySelector('#local-next-box'),
			timeBox: document.querySelector('#local-time'),
			scoreBox: document.querySelector('#local-score'),
			statusBox: document.querySelector('#local-status')
		};
		this.game = new Game();
		//计时器
		this.timer = '';
		//时间间隔
		this.INTERVAL = 200;
		//已用时
		this.time = 0;
		//时间计数器
		this.timeCounter = 1;
		//socket
		this.socket = socket;
	}

	/**
	 * 统计时间
	 * @return {[type]} [description]
	 */
	totalTime(){
		this.timeCounter++;
		if(this.timeCounter == 5){
			this.time++;
			this.game.setTime(this.time);
			this.timeCounter = 1;
			this.socket.emit('time', this.time);
			// if(this.time % 10 == 0){
			// 	//每各10s生成干扰行
			// 	this.game.addLines(this.randomLines(1));
			// }
		}
	}


	/**
	 * 游戏开始方法
	 * @return {[type]} [description]
	 */
	start(){
		let type = this.randomIndex();
		let dir = this.randomDir();
		this.game.init(this.dom, type, dir);
		this.socket.emit('init', {type: type, dir: dir});
		type = this.randomIndex();
		dir = this.randomDir();
		this.game.nextPerform(type, dir);
		this.socket.emit('next', {type: type, dir: dir});
		this.bingKeyEvent();
		this.bingListenerEvents();
		this.timer = setInterval(this.move.bind(this), this.INTERVAL);
	}

	/**
	 * 游戏结束
	 * @return {[type]} [description]
	 */
	stop(){
		clearInterval(this.timer);
		this.timer = null;
		document.onkeydown = null;
	}

	move(){
		this.totalTime();
		if(!this.game.down()){
			this.game.fixed();//固定方块
			this.socket.emit('fixed');
			let line = this.game.checkClear();//判断销行？
			if(line){
				this.game.setScore(line);//设置分数
				this.socket.emit('line', line);
			}
			if(this.game.checkOver()){
				//游戏结束
				this.game.result(false);
				this.socket.emit('lost', false);
				this.stop();
			}else{
				//产生下一个方块
				let type = this.randomIndex();
				let dir = this.randomDir();
				this.game.nextPerform(type, dir);
				this.socket.emit('next', {type: type, dir: dir});
			}
		}else{
			this.socket.emit('down');
		}
	}

	/**
	 * 随机生成干扰行
	 * @param  {Number} lineNum 行数
	 * @return {Array}         生成的干扰行
	 */
	randomLines(lineNum){
		let lines = [];
		for(let i=0; i<lineNum; i++){
			let line = [];
			for(let j=0; j<10; j++){
				line.push(Math.ceil(Math.random()*2)-1);
			}
			lines.push(line);
		}
		return lines;
	}

	/**
	 * 随机生成一个方块种类随机下标
	 * @return {[type]} [description]
	 */
	randomIndex(){
		return Math.ceil(Math.random() * 7) - 1;
	}

	/**
	 * 随机生成一个方块旋转类型随机下标
	 * @return {[type]} [description]
	 */
	randomDir(){
		return Math.ceil(Math.random() * 4) - 1;
	}

	/**
	 * 绑定键盘事件
	 * @return {[type]} [description]
	 */
	bingKeyEvent(){
		let that = this;
		document.onkeydown = function(e){
			if(37 == e.keyCode){//left
				// console.log('left')
				that.game.left();
				that.socket.emit('left');
			}else if(38 == e.keyCode){//up
				// console.log('up')
				that.game.rotate();
				that.socket.emit('rotate');
			}else if(39 == e.keyCode){//right
				// console.log('right')
				that.game.right();
				that.socket.emit('right');
			}else if(40 == e.keyCode){//down
				// console.log('down')
				that.game.down();
				that.socket.emit('down');
			}else if(32 == e.keyCode){//space
				// console.log('space')
				that.game.fall();
				that.socket.emit('fall');
			}
		}
	}

	bingListenerEvents(){
		let that = this;
		this.socket.on('line', function(data){
			//添加line行干扰
			let lineArr = that.randomLines(data);
			that.game.addLines(lineArr);
			that.socket.emit('addLines', lineArr);
		});

		//监听对方游戏结果，data是Boolean值
		this.socket.on('lost', function(data){
			that.game.result(!data);
			that.stop();
			that.socket.emit('lost', !data);
		});

		this.socket.on('leave', function(){
			document.querySelector('#local-status').innerHTML = '对方掉线...';
			document.querySelector('#remote-status').innerHTML = '已掉线...';
			that.stop();
		})
	}
}

export {Local};